package org.tea.inventory.utils;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Random;

/**
 * @author hanzy
 * @description
 * @date 2023年07月06日 9:43
 */
public class XssfUtils {

    public static String getCellValueStr(Cell cell, String def) {
        if (ObjectUtil.isEmpty(cell))
            return def;
        CellType cellType = cell.getCellType();
        if (cellType.equals(CellType._NONE)
                || cellType.equals(CellType.BLANK) || cellType.equals(CellType.ERROR)) {
            return def;
        } else if (cellType.equals(CellType.STRING)) {
            return cell.getStringCellValue();
        } else if (cellType.equals(CellType.NUMERIC)) {
            return cell.getNumericCellValue() + "";
        } else {
            return def;
        }
    }

    public static Double getBigtoDouble(BigDecimal value) {
        return Convert.toDouble(ObjectUtil.defaultIfNull(value, BigDecimal.ZERO));
    }

    public static Double getCellValueNum(Cell cell, Double def) {
        if (ObjectUtil.isEmpty(cell))
            return def;
        CellType cellType = cell.getCellType();
        if (cellType.equals(CellType._NONE)
                || cellType.equals(CellType.BLANK) || cellType.equals(CellType.ERROR)) {
            return def;
        } else if (cellType.equals(CellType.STRING)) {
            return Convert.toDouble(cell.getStringCellValue(), def);
        } else if (cellType.equals(CellType.NUMERIC)) {
            return cell.getNumericCellValue();
        } else {
            return def;
        }
    }

    public static Integer getCellValueInt(Cell cell, Integer def) {
        if (ObjectUtil.isEmpty(cell))
            return def;
        CellType cellType = cell.getCellType();
        if (cellType.equals(CellType._NONE)
                || cellType.equals(CellType.BLANK) || cellType.equals(CellType.ERROR)) {
            return def;
        } else if (cellType.equals(CellType.STRING)) {
            return Convert.toInt(cell.getStringCellValue(), def);
        } else if (cellType.equals(CellType.NUMERIC)) {
            return Convert.toInt(cell.getNumericCellValue());
        } else {
            return def;
        }
    }

    public static Date getCellValueDate(Cell cell, Date def) {
        if (ObjectUtil.isEmpty(cell))
            return def;
        CellType cellType = cell.getCellType();

        if (cellType.equals(CellType._NONE)
                || cellType.equals(CellType.BLANK) || cellType.equals(CellType.ERROR)) {
            return def;
        } else if (cellType.equals(CellType.STRING)) {
            String dateStr = cell.getStringCellValue();
            dateStr = dateStr.substring(0, 10).replaceAll("\\.", "-").replaceAll("\\/", "-");
            return DateUtil.parse(dateStr, "yyyy-MM-dd");
        } else {
            Date val = cell.getDateCellValue();
            if (ObjectUtil.isEmpty(val))
                return def;
            return val;
        }
    }

    public static Long getCellValueLong(Cell cell, Long def) {
        if (ObjectUtil.isEmpty(cell))
            return def;
        org.apache.poi.ss.usermodel.CellType cellType = cell.getCellType();
        if (cellType.equals(CellType._NONE)
                || cellType.equals(CellType.BLANK) || cellType.equals(CellType.ERROR)) {
            return def;
        } else if (cellType.equals(CellType.STRING)) {
            return Convert.toLong(cell.getStringCellValue(), def);
        } else if (cellType.equals(CellType.NUMERIC)) {
            return Convert.toLong(cell.getNumericCellValue());
        } else {
            return def;
        }
    }

    public static String newVerifyCode() {
        String letterStr = "a,b,c,d,e,f,g,h,j,k,m,n,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,U,V,W,X,Y,Z,1,2,3,4,5,6,7,8,9";

        int i = 0;
        Random r = new Random();

        String VerifyCode = "";

        for (i = 0; i < 4; i++) {
            String[] A = letterStr.split(",");
            int n = A.length;
            int k = r.nextInt(n - 1);
            String a = A[k];
            VerifyCode += a;
        }

        return VerifyCode;
    }

}
